import { getUserData } from '@/utils/IndexedDB.js';
const excessTime = {
    namespaced: true,
    state: {
        //zhanghao结束时间
        endTime:'',
        //剩余时间
        remainder:[],
        
        leftTime:'',
        userinfo:'',
    },
    getters: {
        getexcessTime: (state) => {
           return state.remainder
            
        },
        getleftTime:(state) => {
            return state.leftTime
             
         },
         getUserInfo:(state)=>{
             return state.userinfo
         }
    },
    mutations: {
         setExcessTime: async(state) => {
             if(localStorage.getItem('ms_type')){
                
                await getUserData('userBase', 'infoBase', 0).then((res) => {
                    
                        state.endTime = res.endTime;
                   
                    
                    // console.log(state.endTime)
                });
             }
           
        
             //获取当前时间
             let date = new Date();
             let now = date.getTime(); 
             //设置截止时间 

             let endDate = new Date(state.endTime);
            //  console.log(endDate)
             let end = endDate.getTime();
             //获取截止时间和当前时间的时间差
             state.leftTime = end-now;
             //定义变量 d,h,m,s分别保存天数，小时，分钟，秒
             let d,h,m,s;
             //判断剩余天数，时，分，秒
             if (state.leftTime>=0) {
                 d = Math.floor(state.leftTime/1000/60/60/24);
                 h = Math.floor(state.leftTime/1000/60/60%24);
                 m = Math.floor(state.leftTime/1000/60%60);
                 s = Math.floor(state.leftTime/1000%60);
                 state.remainder.push(d,h,m,s)
             }
 
            
 
        
             localStorage.setItem('userTime',state.leftTime)
            //  setTimeout(countTime,1000);
             
         },
         setEndTime:(state,data)=>{
             state.endTime=data
         },
         setUserInfo:(state,data)=>{
            state.userinfo=data
        },
        removeremainder:(state,data)=>{
            state.remainder=data
        },
        removeendTime:(state,data)=>{
            state.endTime=data
        },
        
       
    },
    actions: {
        asyncRemainder(context){
            context.commit(getremainder)
        }
    }
};

export default excessTime;